package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;

public class Planet extends Start {
    public Start sun;

    public Planet(Start sun,String imageKey,double AU,double e,double T ) {
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxisByAU(AU);
        this.shortAxis = getShortAxisByE(e);
        this.x = Constant.CONSTANT_WINDOW_WIDTH / 2 + longAxis;
        this.y = Constant.CONSTANT_WINDOW_HEIGHT / 2;
        this.theta = 0.0;
        this.speed = getSpeedByT(T);
        this.sun = sun;


    }

    public Planet(Start sun,String imageKey,double AU,double e,double T,int width, int height) {
        this(sun,imageKey,AU,e,T);
        this.width = width;
        this.height = height;
    }
    public Planet(Start sun,String imageKey,double AU,double e,double T, int width, int height,boolean smallPlanets,boolean venus) {
        this(sun,imageKey,AU,e,T,smallPlanets);
        this.width = width;
        this.height = height;
         this.venus = venus;
    }
    public Planet(Start sun,String imageKey,double AU,double e,double T, boolean smallPlanets) {
        this(sun,imageKey,AU,e,T );
        this.smallPlants = smallPlanets;
    }

    private double getSpeedByT(double t) {
        return 0.1 * 365 / t;
    }

    public Planet() {
    }
    private int getLongAxisByAU(double AU){
        return (int)(100 * AU /2)  ;
    }
    //半长轴*根号下(1-e*e)
    public int getShortAxisByE(double e){
        return (int)(this.longAxis * Math.sqrt(1 - e * e)) ;
    }

    @Override
    public void draw(Graphics g) {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        if (!smallPlants  ){
            drawTrace(g);
        }



        g.drawImage(img, x, y, this.width,this.height,null);
        g.setColor(color);
        move();
    }

    private void drawTrace(Graphics g){
        int x = (int) (this.sun.x - this.longAxis + this.sun.width / 2  + (Math.sqrt(Math.pow(longAxis, 2) - Math.pow(shortAxis, 2)))) ;
        int y = this.sun.y - this.shortAxis + this.sun.height / 2  ;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y, width, height);
    }

    @Override
    public void move() {
        if (!venus){
            x = (int)((longAxis * Math.sin(theta)) + sun.x + sun.width/2 - this.width /2  + (Math.sqrt(Math.pow(longAxis, 2) - Math.pow(shortAxis, 2)))) ;
            y = (int)(shortAxis * Math.cos(theta)) + sun.y + sun.height/2 - this.height /2  ;
        }else {
            x = (int)((longAxis * Math.cos(theta)) + sun.x + sun.width/2 - this.width /2  + (Math.sqrt(Math.pow(longAxis, 2) - Math.pow(shortAxis, 2)))) ;
            y = (int)(shortAxis * Math.sin(theta)) + sun.y + sun.height/2 - this.height /2  ;
        }

        theta += speed;
    }
}
